//
//  LayerReady.cpp
//  lhq
//
//  Created by codin on 14-7-24.
//
//

#include "LayerReady.h"
#include "LayerLogin.h"
#include "LayerTable.h"
#include "GamePlayer.h"
//#include "D:\mywork\pomelo_c\game_lhq\cocos2d\external\libpomelo\deps\jansson\src\jansson.h"
#include "jansson.h"

USING_NS_CC;

Scene* LayerReady::createScene() {
    auto scene = Scene::create();
    auto layer = LayerReady::create();
    scene->addChild(layer);
    return scene;
}

bool LayerReady::init(){
    if (!Layer::init()){
        return false;
    }
    
    auto director = Director::getInstance();
    
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    auto bgSp = Sprite::create("texture/zhuomian.jpg");
    bgSp->setPosition(Vec2(origin.x + visibleSize.width - bgSp->getContentSize().width/2,origin.y + bgSp->getContentSize().height/2));
    this->addChild(bgSp);
    
    auto readyBtn=MenuItemImage::create("texture/table/ready_btn1.png","texture/table/ready_btn1.png",CC_CALLBACK_1(LayerReady::ready,this));
    readyBtn->setPosition(visibleSize.width/2,visibleSize.height/2-50);
    auto menu = Menu::create(readyBtn,nullptr);
    menu->setPosition(0,0);
    this->addChild(menu,30);
    
    return true;
}
void LayerReady::onGameStart(cocos2d::CCNode *node, void *resp)
{

	PomeloReponse* ccpomeloresp = (PomeloReponse*)resp;

	GamePlayer::getInstance()->setPomeloReponse(ccpomeloresp);
	Director::getInstance()->replaceScene(LayerTable::createScene());

}


void LayerReady::ready(Ref* sender){

	//std::string username="123";
	Pomelo *pPomelo=Pomelo::getInstance();

	const char *route = "manage.tableHandler.entry";
	json_t *msg = json_object();
	json_t *str = json_string("0");
	json_t *str2 = json_string("1");

	json_object_set(msg, "tableType", str);
	json_object_set(msg,"tableLevel",str2);
	json_decref(str);

	pPomelo->addListener("onGameStart",this, callfuncND_selector(LayerReady::onGameStart));

	pPomelo->request(route,msg,this,callfuncND_selector(LayerReady::requstReadyCallback) );
}

 void LayerReady::requstReadyCallback(cocos2d::CCNode *node, void *resp)
{
	PomeloReponse* ccpomeloresp = (PomeloReponse*)resp;

	int code = json_integer_value(json_object_get(ccpomeloresp->docs,"code"));
	
	if(code==200)
	{
		
	}
}


